Ext.onReady(function() {
    var cbstore = new Ext.data.Store({
    	        singleton:true,
    	        autoDestroy:true,
    	        fields: ["projectName", "id"],
    	        proxy:{
    	            type:'ajax',
    	            url:jypath+'/estate/project/findProjectList?resourcesCode=menu_0003_0001&resourcesName=resName_pro_list',
    	            reader:{
                        type: 'json', 
                        root: 'root'  //数据
                   }
    	        },
    	        autoLoad:true
    });
	var fs = new Ext.form.FieldSet({  
	    title:'查询条件',  
	    collapsible: true,//可折叠  
	    layout:'vbox',//vbox布局  
	    height:50,//设置FieldSet的高度  
	    defaults:{  
	        border:false        //去掉panel的边框  
	    },
	    fieldDefaults: {
	        labelWidth: 80,
            labelAlign: "right",
            margin: "5 0 0 0"
	    },
	    items:[
	    	//权限资源隐藏字段
    		{xtype:"textfield", id: "resourcesCode", name:"resourcesCode", value: resourcesCode, hidden: true},
    		{xtype:"textfield", id: "resourcesName", name:"resourcesName", value: "resName_group_list", hidden: true},
    		{  
	        xtype:'panel',  
	        width:'100%',  
	        flex:1,  
	        defaults:{  
	            labelWidth:60  
	        },        
	        items:[{  
	            xtype:'combobox',  
	            style: 'position:absolute;left:15px;',  
	            id:'cbstore',  
	            store: cbstore,
	            displayField: "projectName",           
	            valueField: "id",  
	            fieldLabel:'所在项目',  
	            width:200,
	            queryMode:'local',
	            typeAhead: true
	            
	        },{  
	            xtype:'button',  
	            style: 'position: absolute; left: 220px; top: 5px;',  
	            width:50,  
	            text:'查询',  
	            handler:function(){
	            	gridStore.load();
	            	var pagingTool=Ext.getCmp("pagingTool");
  	        		pagingTool.moveFirst();//跳至第一页
	            }  
	        }]  
	    }]  
	});
    var gridStore = Ext.create('Ext.data.Store', {
        fields: ["id","groupName","projectName", "groupTotal", "currentTotal","hisTotal","groupStatus"],
        pageSize: 10,  //页容量5条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/marketing/salesGroup/list',
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            }
        },
        autoLoad: true  //即时加载数据
    });
    
    gridStore.on("beforeload",function(){
        Ext.apply(gridStore.proxy.extraParams, {
        	projectId: Ext.getCmp('cbstore').getValue(),
        	resourcesName: Ext.getCmp('resourcesName').getValue()
    	});
        var newResCode = Ext.getCmp('resourcesCode').getValue() + ",menu_0003_0001";
        gridStore.proxy.extraParams.resourcesCode = newResCode;
    });
    
    var grid = Ext.create('Ext.grid.Panel', {
        store: gridStore,
        columnLines: true,
        columns: [
        {  
            text : "查看",  
          	dataIndex: 'id',
          	minWidth:50,
            renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
                var rec = grid.getStore().getAt(rowIndex);
                var html = "";
                html+='<a href="javascript:detail('+value+')">查看</a>';
                return html;
            }
        },{
            text: '项目组名称',
            dataIndex: 'groupName',
            width:500
            	
        },{
            text     : '所在项目', 
            dataIndex: 'projectName'
        },{
            text     : '组员人员',
            dataIndex: 'groupTotal'
        },{
            text     : '当前组员数',
            dataIndex: 'currentTotal'
        },{
            text     : '历史组员数',
            dataIndex: 'hisTotal'
        },{
            text     : '项目组状态',
            dataIndex: 'groupStatus',
            renderer : function(val) {
                if (val == 0) {
                    return '正常';
                } else if (val == 1) {
                    return '已关闭';
                }
                return val;
            }
        }],
        selModel: {
            selType: 'rowmodel'
        },
        title: '项目组列表',
        viewConfig: {
            stripeRows: true
        },
        dockedItems: [{  
            xtype: 'pagingtoolbar',  
            store: gridStore,   // GridPanel中使用的数据  
            dock: 'bottom',  
            displayInfo: true,  
            emptyMsg:"没有数据",  
            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据",
            id:'pagingTool'
        }
        ], tbar:[
                 {hidden:salesGroupAdd,text:'新增',icon:jypath+'/resources/ExtJSicons/tag/tag_blue_add.png',handler:showAlert},
                 {hidden:salesGroupEdit,text:'编辑',icon:jypath+'/resources/ExtJSicons/tag/tag_blue_edit.png',handler:editSelesGroup},
                 {hidden:salesGroupCloseGroup,text:'关闭项目组',icon:jypath+'/resources/ExtJSicons/tag/tag_blue_delete.png',handler:deleteSelesGroup},
                 {text:'查看',icon:jypath+'/resources/ExtJSicons/tag/tag_orange.png',handler:openWindow}
                 ]
    });
	
	
	var viewport = Ext.create('Ext.Viewport', {
		layout : 'border',
		items:[
		  {   
			  region:'north',
			  items:fs
		  },{
			  region:'center',
			  items:grid 
		  }    
		]
	});
	
	function openWindow(){
		
		var record = grid.getSelectionModel().getLastSelected();
		if(record==undefined){
			Ext.MessageBox.alert("系统提示", "请选中记录!");
		}else{
			//创建窗口
			//弹出详情框
			 var windowInfo=Ext.create('marketing.salesGroup.view',{
				 salesGroupId:record.data.id
			 });
			 windowInfo.show();
		}
	}
	
	function editSelesGroup(){
		var record = grid.getSelectionModel().getLastSelected();
		if(record==undefined){
			Ext.MessageBox.alert("系统提示", "请选中记录!");
		}else{
			  //已关闭的项目组不能编辑
			  if(record.data.groupStatus==1){
				  Ext.MessageBox.alert("系统提示", "已关闭的项目组不能编辑!");
			  }else{
				 var url = "/marketing/salesGroup/addFrom?salesGroupId="+record.data.id+"&1=1";
				 parent.openMenu("0","addGroup"+record.data.id,"","编辑项目组",url);

			  }
			  
			
		}
	}
	
	function deleteSelesGroup(){
		var record = grid.getSelectionModel().getLastSelected();
		if(record==undefined){
			Ext.MessageBox.alert("系统提示", "请选中记录!");
		}else{
			  Ext.MessageBox.confirm("提示","是否关闭项目组?",function(btn,txt){
              	if(btn=="yes"){
              		Ext.Ajax.request({
              			 url: jypath+'/marketing/salesGroup/closeGroup',
                           params: { id: record.data.id},
                           method: 'POST',
                           callback: function (options, success, response) {
                          	 if (success) {
                          		 Ext.MessageBox.alert("系统提示","关闭项目组成功");
                          		 gridStore.reload();
                          	 }else{
                          		 Ext.MessageBox.alert("系统提示","关闭项目组失败！");
                          	 }
                           }
              		});
                  }
              });
			
		}
	}
	
	function showAlert (){
		var url ="/marketing/salesGroup/addFrom";
		parent.openMenu("0","addGroup","","新增项目组",url);
	}
});

function detail(id){
	var windowInfo=Ext.create('marketing.salesGroup.view',{
		 salesGroupId:id
	});
	windowInfo.show();
}
